﻿#ifndef CAMERABASLER_H
#define CAMERABASLER_H

#include "camera.h"
#include <pylon/PylonIncludes.h>
#include <pylon/BaslerUniversalInstantCamera.h>
#include <pylon/_BaslerUniversalCameraParams.h>
#include <QElapsedTimer>

using namespace Pylon;
using namespace GenApi;
using namespace Basler_UniversalCameraParams;


//回调函数采集类
class MyImageEventHandler:public QObject,public Pylon::CBaslerUniversalImageEventHandler
{
    Q_OBJECT
public:
    MyImageEventHandler();

    //图像采集回调
    virtual void OnImageGrabbed( Pylon::CBaslerUniversalInstantCamera& camera, const Pylon::CBaslerUniversalGrabResultPtr& grabResult ) override;
signals:
    void imageGrabbed(ImageInfo imgInfo);
};

//相机事件类
class MyConfigurationEventHandler:public QObject,public Pylon::CConfigurationEventHandler
{
    Q_OBJECT
public:
    MyConfigurationEventHandler();
    //相机掉线事件回调
    virtual void OnCameraDeviceRemoved( CInstantCamera& camera ) override;
signals:
    void cameraRemoved();
};
class MyEventHandler :public QObject, public CBaslerUniversalCameraEventHandler
{
     Q_OBJECT
public:
    MyEventHandler();
    //相机掉线事件回调
    virtual void OnCameraEvent( CBaslerUniversalInstantCamera& camera, intptr_t userProvidedId, GenApi::INode* /* pNode */) override;
signals:
    void sigCameraTrigger();
};


class CameraBasler : public Camera
{
    Q_OBJECT
public:
    CameraBasler(const std::string name);
    virtual ~CameraBasler();
    // Camera interface
public slots:
    virtual void initialize()override;
    virtual void openCamera()override;
    virtual void startGrabbing()override;
    virtual void stopGrabbing()override;
    virtual void closeCamera()override;
    virtual void releaseCamera() override;
    virtual void preProcessImage(const ImageInfo& imgInfo)override;

    virtual void readCameraROI(const std::string& camName)override;
    virtual void readCameraWhiteBalance(const std::string& camName)override;
    virtual void readCameraBrightness(const std::string& camName)override;
    virtual void readCameraTrigger(const std::string& camName)override;
    virtual void readCameraTrans(const std::string& camName)override;

    virtual void setCameraROI(const std::string& camName, const CameraROI &roi)override;
    virtual void setCameraWhiteBalance(const std::string& camName, const CameraWhiteBalance &cw)override;
    virtual void setCameraBrightness(const std::string& camName,  const CameraBrightness &cb)override;
    virtual void setCameraTrigger(const std::string& camName,  const CameraTrigger &ct)override;
    virtual void setCameraTrans(const std::string& camName, const CameraTrans &cts)override;


    virtual void triggerSoftware()override;//软触发
    virtual void setTriggerModeOff()override;
    virtual void setTriggerModeOn()override;
    virtual void setTriggerModeSoftware()override;
    virtual void setTriggerModeLine()override;
    virtual void setTriggerDelay(const std::string& camName, const FloatValue &delay)override;

    //ROI
    virtual void setWidth(const std::string& camName, const IntValue& width)override;
    virtual void setHeight(const std::string& camName, const IntValue& height)override;
    virtual void setOffsetX(const std::string& camName, const IntValue& off_x)override;
    virtual void setOffsetY(const std::string& camName, const IntValue& off_y)override;
    //Trans
    virtual void setReverseX(const std::string& camName, const bool& reverse_x)override;
    virtual void setReverseY(const std::string& camName, const bool& reverse_y)override;
    //white balance
    virtual void setWhiteBalanceR(const std::string& camName, const IntValue &wb_r)override;
    virtual void setWhiteBalanceG(const std::string& camName, const IntValue &wb_g)override;
    virtual void setWhiteBalanceB(const std::string& camName, const IntValue &wb_b)override;
    //Brightness
    virtual void setExposureTime(const std::string& camName, const FloatValue &exp)override;
    virtual void setExposureAutoMode(const std::string& camName, const IntValue &expMode)override;
    virtual void setGain(const std::string& camName, const FloatValue &exp)override;

    virtual void getOneFrame() override;

    virtual bool isColor(int pixelFormat) override{return false;}
    virtual bool isMono(int pixelFormat) override{return false;}

private:
    // The pylon camera object.
    Pylon::CBaslerUniversalInstantCamera m_camera;
    MyImageEventHandler* m_imageHandler;
    MyConfigurationEventHandler *m_configHandler;
    MyEventHandler* m_eventHandler;
    int64_t m_payLoadSize = 0;

};

#endif // CAMERABASLER_H
